package homework1;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @Author han
 * 使用CompletableFuture
 */
public class ByCompletableFuture {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 开始时间
        long start=System.currentTimeMillis();

        CompletableFuture<Integer> task = CompletableFuture.supplyAsync(ByCompletableFuture::sum);

        CompletableFuture<Integer> future = task.whenComplete((v, e) -> {
            System.out.println("计算中...");
        });

        // 获取线程返回值
        System.out.println("计算结果为："+future.get());
        System.out.println("使用时间："+ (System.currentTimeMillis()-start) + " ms");
        try {
            while (!task.isDone()) {

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("退出主线程");
    }

    private static int sum() {
        return fibo(36);
    }

    private static int fibo(int a) {
        if ( a < 2)
            return 1;
        return fibo(a-1) + fibo(a-2);
    }
}
